package cn.cy.dao;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

public class  BaseDao<T> extends HibernateDaoSupport {
        public BaseDao(){}
    public void save(T t){
        getHibernateTemplate().save(t);
    }

    public void delete(T t){
        getHibernateTemplate().delete(t);
    }

    public void update(T t){
        getHibernateTemplate().update(t);
    }

    public List<T> findAll(String hql)
    {
        return getHibernateTemplate().find(hql);
    }

    public T findById(final Class clazz ,final Serializable id){
        return this.getHibernateTemplate().execute(new HibernateCallback<T>() {
            @Override
            public T doInHibernate(Session session) throws HibernateException, SQLException {
                return (T)session.get(clazz,id);
            }
        });
    }
    public List<T> findAll(Class clazz){
        return this.getHibernateTemplate().find("from "+clazz.getName());
    }
}
